<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 17-11-22
 * Time: 下午3:57
 */

namespace api\modules\unionpay\controllers;


use common\components\tools\Tools;
use common\models\Unionpay3002SetLog;
use wodrow\yii\rest\Controller;
use yii\base\ErrorException;

class BackController extends Controller
{
    /**
     * 获取人脸识别3002的认证回调数据
     * @desc 主要为根据手机设备上采集的身份证号，中文姓名，样本照片，验证本人的相似度的回调。
     */
    public function actionSet3002()
    {
        $data = $_REQUEST;
//        $data = array (
//            'signature' => 'J88d5D8DkNX/8UATmSzKRs5r53VkBiQ5yPxvdJJ8wYjxd8gxyWGP1OJlPL+nPCTnBO1+BIk+uifuvMrAWJsBxztmJNLTcH1jh0tIBq4jwdvSDvOXzH3DBaZi9QYfic1xET+345r4RhGzW/ARYa9VTuQQIy46OH0yOvolQk3HxzY=',
//            'respData' => 'eyJyZXNwTXNnIjoi6K6k6K+BL+afpeivouaIkOWKnyIsInNjb3JlIjoiOTUuMjgiLCJidXNpVHlwZSI6IjMwMDIiLCJiZ1JldFVybCI6Imh0dHA6Ly9hcGkucWljaGUxMDAuY29tL3VuaW9ucGF5L2JhY2svc2V0MzAwMiIsIm9yZGVySWQiOiIyMDE3MTEyNzE3MDU4NTExOTY5MjIzIiwiZGV2aWNlT3MiOiJBbmRyb2lkIiwibWVyTm8iOiI3NTEyMjE3MDUxOTgwMDMiLCJvcmRlckRhdGUiOiIyMDE3MTEyNyIsInJlc3BDb2RlIjoiMDAwMDAwMDAiLCJ1c3JOYW1lIjoi5YiY5ZCv5ZCbIn0=',
//            'merNo' => '751221705198003',
//        );
        $data['_respData'] = Tools::objectToArray(json_decode(base64_decode($data['respData'])));
        if ($data['_respData']['respMsg'] == '认证/查询成功'){
            if (!$data['_respData']['orderId']){
                throw new ErrorException("没有orderId");
            }
            \Yii::$app->cache->set('orderId_'.$data['_respData']['orderId'], $data, 3600);
            $log = Unionpay3002SetLog::findOne(['order_id' => $data['_respData']['orderId']]);
            if (!$log){
                throw new ErrorException("3002 没有记录请求");
            }
//            $log->u_name = $data['_respData']['usrName'];
            $log->r_data = serialize($data);
            $log->r_at = time();
            $log->score = $data['_respData']['score'];
            $log->save();
        }
    }
}